<template>
  <div class="phone-container">
    <div class="top">
      <div class="title">盘点填报 <span></span></div>
    </div>
    <el-card v-if="!state.hasPermission">
      <p>您没有权限进行库存盘点操作</p>
    </el-card>
    <el-card v-else-if="state.kcLock">
      <el-alert style="margin-top: 10px" title="库存被锁定,无法填报" type="error" effect="dark" center :closable="false"  />
    </el-card>
    <el-card v-else class="pdpanel">
      <p>仓库名称：{{state.kcObj.cwhname}}</p>
      <p>物品编码：{{state.kcObj.cinvcode}}</p>
      <p>物品名称：{{state.kcObj.cinvname}}</p>
      <p>规格型号：{{state.kcObj.cinvstd}}</p>
      <p v-if="state.kcObj.cbatch">批号：{{state.kcObj.cbatch}}</p>
      <p>物品单位：{{state.kcObj.ccomunitname}}</p>
      <p>现存数量：<span style="color: #4396EB">{{state.kcObj.iquantity}}</span></p>
      <p>上次盘点：<span style="color: #4396EB" v-if="state.kcObj.pduser">{{state.kcObj.pduser}} (<DateFormat :value="state.kcObj.lasttbdate" :format="'YYYY-MM-DD HH:mm'"></DateFormat>)</span> <span style="color: #4396EB"  v-else>无</span></p>
      <p><span>盘点数量：</span> <el-input v-model="state.form.pdcount" placeholder="盘点数量" style="width: 240px" /></p>
      <p style="text-align: center">
        <el-button size="default" type="primary" @click="savepd">
          <el-icon><ele-Edit /></el-icon>保存盘点
        </el-button>
        <el-button size="default" type="success" @click="scan" v-if="state.wxReady">
          <el-icon><ele-Camera /></el-icon>扫一扫
        </el-button>
      </p>
    </el-card>
  </div>
</template>

<script setup name="pdedit">
import request from '/@/utils/request';
import {reactive, ref, onMounted} from "vue";
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import PermissionType from "/@/utils/PermissionType";
import { useUserInfo } from '/@/stores/userInfo';
import {auth} from "/@/utils/authFunction";
const route = useRoute();
const router = useRouter();
const userInfo = useUserInfo();

const state = reactive({
  kcLock:false,
  kcid: 0,
  userName : userInfo.userInfos.userName,
  permissionType:PermissionType,
  hasPermission : false,
  kcObj : {},
  form:{
    pdcount:undefined
  },

  wxReady : false
})
const loadKcLock = ()=>{
  request({
    url: '/cangku/kclock',
    data: {cwhcode : state.kcObj.cwhcode}
  }).then(res => {
    const { data } = res
    state.kcLock = data.lockstatus === 1
  })
}
const getKcData = ()=>{
  request({
    url: '/currentstore/getcurrentstore',
    data: {id : state.kcid}
  }).then(res => {
    const { data } = res
    state.kcObj = data;
    loadKcLock();
  })
}
const savepd = ()=>{
  if(isNaN(state.form.pdcount)){
    ElMessage.error({
      message : '请填写数字',
      offset : 60
    })
    return false;
  }
  request({
    url: '/currentstore/savecurrentstore',
    data: {id : state.kcid, pdquantity : state.form.pdcount}
  }).then(res => {
    const { code } = res
    if(code !== '200'){
      ElMessage.error({message : res.msg, offset : 60})
    }else{
      ElMessage.success({message : res.msg, offset : 60})
    }
  })
}
const setWxReady = ()=>{
  request({
    url: "/user/wxreadyconfig?url="+window.location.href.split('#')[0],
    data:{}
  }).then( res =>{
    const datad = res.data
    wx.config({
      beta: true,
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来
      appId: datad.appid, // 必填，公众号的唯一标识
      timestamp: datad.timestamp, // 必填，生成签名的时间戳
      nonceStr: datad.noncestr, // 必填，生成签名的随机串
      signature: datad.signature,// 必填，签名，见附录1
      jsApiList: ['checkJsApi', 'scanQRCode', 'chooseImage', 'getLocation']
    });
    wx.ready(function() {
      state.wxReady = true;
    });
    wx.error(function (res) {
      alert(JSON.stringify(res))
    });
  })
}
const scan = () =>{
  try {
    wx.scanQRCode({
      desc: '扫码盘点',
      needResult: 0, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
      scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是条形码（一维码），默认二者都有
      success: function(res) {
        // var result = res.resultStr;//当needResult为1时返回处理结果
        // alert(result)
      },
      error: function(res) {
        if (res.errMsg.indexOf('function_not_exist') > 0) {
          alert('版本过低请升级')
        }
      }
    });
  }catch (error){
    alert(error)
  }
}
onMounted(()=>{
  state.kcid = route.query.kcid || 0;
  state.hasPermission = auth(state.permissionType.pddata_pandian)
  if(state.hasPermission){
    setWxReady();
    getKcData();
  }
})
</script>

<style scoped lang="scss">
.phone-container{
  .pdpanel{
    p{
      padding-bottom: 10px;
      font-weight: bold;
      color: #1f1f1f;
    }
  }
}
</style>
